﻿/* 
										Name :   Đỗ Hoàng Phương
									  Input  :   stdio_source.cpp
									  Output :   stdio_source.txt
								Source Code  :   stdio_HoangPhuong_cpp.cpp
*/

#include <stdio.h>

char *ReadFileInputData(char *temp, int size);

// lenght char
int lenght(char *string)
{
	int i = 0;
	while (string[i])
		i++;

	return i;
}

// a+b
char *AddChar(char *path, char *temp)
{
	int i = lenght(path);//3 +10
	int j = lenght(temp);
	path[i];

	char *s = new char[i + j];

	for (int k = 0; k < i; k++)
	{
		s[k] = path[k];
	}

	for (int k = 0; k < j; k++)
	{
		s[i + k] = temp[k];
	}
	s[i + j] = '\0';

	return s;
}

// Insert
char *InsertString(char *path, char *temp, int location)
{
	int m = lenght(path);
	int n = lenght(temp);

	char *s = new char[m + n];

	int i;
	for (i = 0; i < location; i++)
	{
		s[i] = path[i];
	}
	for (int j = 0; j < n; j++)
	{
		s[i] = temp[j];
		i++;
	}
	for (int j = location; j < lenght(path); j++)
	{
		s[i] = path[j];
		i++;
	}
	s[i] = '\0';

	return s;
}

// Inspect: #include
bool FindString(char *data, char *find, int loacationFind)
{
	if (lenght(data) - loacationFind < lenght(find))
		return false;
	for (int i = loacationFind; i < lenght(find); i++)
	{
		if (data[i] != find[i - loacationFind])
			return false;
	}

	return true;
}

// Size file
int SizeFile(char *path)
{
	FILE *sizeFile = fopen(path, "r");
	fseek(sizeFile, 0, SEEK_END);
	int size = ftell(sizeFile);
	fclose(sizeFile);

	return size;
}

// Insert into data
char *ProcessorHeaderInclude(char *s)
{
	for (int m = 0; m < lenght(s); m++)
	{
		int n = m;
		while (s[n] && s[n] != '\n')
		{
			n++;
		}
		int i = m, k = 0, j = 0;

		while (s[i] == ' ' && i <= n)
		{
			i++;
		}
		if (s[i] == '#' && i <= n)
		{
			if (FindString(s, "include", i + 1))
			{
				j = i + 8;	// #include
				while (s[j])
				{
					if (s[j] == '"')
					{
						j++;
						k = 0;
						char *temp = new char[100];
						while (s[j] != NULL && s[j] != '"')
						{
							temp[k] = s[j];
							k++;
							j++;
						}
						temp[k] = '\0';
						if (fopen(temp, "r"))
						{
							for (int y = 0; y < n - m; y++)
							{
								for (int t = i; t < lenght(s); t++)
								{
									s[t] = s[t + 1];
								}
							}
							int size = SizeFile(temp);
							char *s2 = new char[size];
							s2 = ProcessorHeaderInclude(ReadFileInputData(temp, size));
							n = m + lenght(s2) - 1;
							s = InsertString(s, s2, i);
							delete temp;
							break;
						}
					}
					j++;
				}
			}
		}
		m = n;
	}

	return s;
}

// Input data
char *ReadFileInputData(char *temp, int size)
{
	char *s = new char[size];
	s = "";
	char *x = new char[size];

	FILE *readFile;
	readFile = fopen(temp, "r");

	if (!readFile)
	{
		printf("\nFile nguon co van de");
	}
	else
	{
		while (fgets(x, size, readFile) != NULL)
		{
			s = AddChar(s, x);
		}
		fclose(readFile);
	}

	delete x;
	return s;
}

// Main
int main()
{

	int size = SizeFile("stdio_source.cpp");

	char *file = new char[size];
	file = ReadFileInputData("stdio_source.cpp", size);
	file = ProcessorHeaderInclude(file);

	FILE *writeFile = fopen("stdio_source.txt", "w");
	fprintf(writeFile, "%s", file);
	fclose(writeFile);
	
	file = NULL;
	delete file;
	return 0;
}